using AutoMapper;
using System.Collections.Generic;
using System.Linq;
using Pay.TO;

namespace Pay.Service {
	public static class ProjectionsExtension {
		/// <summary>
		/// 项目的类型，使用DTO
		/// </summary>
		/// <typeparam name="TTarget">DTO映射</typeparam>
		/// <param name="source">实体源</param>
		/// <returns>>DTO映射</returns>
		public static TTarget ToDtoObject<TTarget>(this object source) {
			return Mapper.Map<TTarget>(source);
		}
		/// <summary>
		/// 项目的类型，使用DTO
		/// </summary>
		/// <typeparam name="TTarget">DTO映射</typeparam>
		/// <param name="source">实体源</param>
		/// <returns>>DTO映射</returns>
		public static TTarget ToDtoObject<TTarget>(this object source, TTarget target)
			where TTarget : class, new() {
			return Mapper.Map(source, target);
		}

		public static List<TTarget> ToDtoList<TTarget>(this object source) {
			return Mapper.Map<List<TTarget>>(source);
		}

		public static List<TTarget> ProjectTo<TSource, TTarget>(this IQueryable<TSource> source) {
			return Mapper.Map<List<TTarget>>(source.ToList());
		}

		public static List<TTarget> ProjectTo<TSource, TTarget>(this ICollection<TSource> source) {
			return Mapper.Map<List<TTarget>>(source.ToList());
		}

		public static List<TTarget> ProjectTo<TSource, TTarget>(this IEnumerable<TSource> source) {
			return Mapper.Map<List<TTarget>>(source.ToList());
		}
		/// <summary>
		/// 转化分页对象
		/// </summary>
		/// <typeparam name="TTarget"></typeparam>
		/// <param name="source"></param>
		/// <returns></returns>
		public static PagedResult<TTarget> ToDtoPaged<TSource, TTarget>(this PagedResult<TSource> source) {
			return Mapper.Map<PagedResult<TTarget>>(source);
		}
	}
}
